import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import '../../models/book/BookQuizResult.dart';
import '../../pages/common/fireWorks.dart';

// ignore: must_be_immutable
class CompleteBookDialog extends StatefulWidget {
  String btitle;
  final String bid;
  final box = Hive.box('appconfig');
  BookQuizResult results;
  bool firstAttempt;
  CompleteBookDialog(
      {Key? key,
      required this.btitle,
      required this.bid,
      required this.results,
      required this.firstAttempt})
      : super(key: key);

  @override
  _CompleteBookDialog createState() => _CompleteBookDialog();
}

class _CompleteBookDialog extends State<CompleteBookDialog> {
  Widget build(BuildContext context) {
    var currentScore = widget.results.percentage!;
    var preScore = widget.results.maxScore!.percentage;
    var points = widget.results.points ?? 0;
    var _points = widget.box.get('UserPoints');
    if (points > 0 && _points != null) {
      widget.box.put("UserPoints", _points + points);
    }
    return AlertDialog(
        content: Container(
      height: 500,
      width: MediaQuery.of(context).size.width > 500
          ? 500
          : MediaQuery.of(context).size.width,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          SizedBox(height: 15),
          if (currentScore == 100)
            Text(
              '✨ Excellent job! You are awesome! ✨'.tr,
              style: TextStyle(
                  fontSize: 20,
                  color: Theme.of(context).colorScheme.onPrimary,
                  fontWeight: FontWeight.bold),
            ),
          if (currentScore < 100 && currentScore >= 75)
            Text(
              '😊 Great job!'.tr,
              style: TextStyle(
                  fontSize: 20,
                  color: Theme.of(context).colorScheme.onPrimary,
                  fontWeight: FontWeight.bold),
            ),
          if (currentScore < 75 && preScore! < 75)
            Text(
              'Try again after re-reading the book. You can do better next time.'
                  .tr,
              style: TextStyle(
                  fontSize: 20,
                  color: Theme.of(context).colorScheme.onPrimary,
                  fontWeight: FontWeight.bold),
            ),
          if (currentScore < 75 && preScore! >= 75)
            Text(
              'Check your work. You can do better next time.'.tr,
              style: TextStyle(
                  fontSize: 20,
                  color: Theme.of(context).colorScheme.onPrimary,
                  fontWeight: FontWeight.bold),
            ),
          SizedBox(height: 8),
          if (points > 0)
            Text(
              'You are rewarded $points points.',
              style: TextStyle(
                  fontSize: 20,
                  color: Theme.of(context).colorScheme.onPrimary,
                  fontWeight: FontWeight.bold),
            ),
          //  ---- will cause App collase
          Expanded(
            child: SizedBox(
              height: 50.0,
              child: Column(
                children: [FireWorks(score: widget.results.percentage)],
              ),
            ),
          ),
          SizedBox(height: 10),
          Row(
            children: [
              Expanded(
                child: SizedBox(
                  height: 350.0,
                  child: ListView.builder(
                      shrinkWrap: true,
                      padding: const EdgeInsets.all(8),
                      itemCount: widget.results.results!.length,
                      itemBuilder: (BuildContext context, int index) {
                        return Center(
                          child: RichText(
                              textAlign: TextAlign.center,
                              text: TextSpan(
                                text:
                                    'Question ${widget.results.results!.keys.toList()[index]}:',
                                style: TextStyle(
                                    fontSize: 20,
                                    color: Theme.of(context)
                                        .colorScheme
                                        .onPrimary),
                                children: [
                                  WidgetSpan(
                                    alignment: PlaceholderAlignment.middle,
                                    child: widget.results.results!.values
                                                .toList()[index] ==
                                            true
                                        ? Icon(Icons.check,
                                            color: Colors.green, size: 30)
                                        : Icon(Icons.close,
                                            color: Colors.red, size: 30),
                                  )
                                ],
                              )),
                          // child: Text(
                          //   'Question ${widget.results.results!.keys.toList()[index]}:  ${widget.results.results!.values.toList()[index] == true ? Icon(Icons.check, color: Colors.green, size: 25) : Icon(Icons.close, color: Colors.red, size: 25)}',
                          //   style: TextStyle(
                          //       fontSize: 20,
                          //       color: Theme.of(context).colorScheme.onPrimary),
                          // ),
                        );
                      }),
                ),
              ),
              // Expanded(
              //   child: SizedBox(height: 350.0, child: SparkleWidget()),
              // ),
            ],
          ),
          MaterialButton(
              color: Theme.of(context).colorScheme.primary,
              onPressed: () {
                // if (widget.firstAttempt == true && currentScore < 75 ||
                //     (currentScore < 75 &&
                //         widget.firstAttempt == false &&
                //         preScore! < 75)) {
                //   _booksController.resetBookStatus(widget.bid);
                // }
                // go to book Page
                Navigator.of(context).pop();
                Navigator.of(context).pop();
              },
              child: Text('Close'.tr))
        ],
      ),
    ));
  }
}
